package com.xhj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhj.client.MemberClient;
import com.xhj.entity.StatisticsDaily;
import com.xhj.mapper.StatisticsDailyMapper;
import com.xhj.service.StatisticsDailyService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 网站统计日数据(StatisticsDaily)表服务实现类
 *
 * @author makejava
 * @since 2021-03-12 09:49:16
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper,StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private MemberClient memberClient;

    @Override
    public void statisticsByDay(String day) {
        // 删除原有的数据
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated",day);
        baseMapper.delete(queryWrapper);

        Integer countRegister = memberClient.countRegister(day);
        Integer countLogin = RandomUtils.nextInt(100,200);
        Integer countView = RandomUtils.nextInt(100,200);
        Integer countCourse = RandomUtils.nextInt(100,200);

        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily
                .setRegisterNum(countRegister)
                .setLoginNum(countLogin)
                .setVideoViewNum(countView)
                .setCourseNum(countCourse)
                .setDateCalculated(day);

        baseMapper.insert(statisticsDaily);
    }

    @Override
    public Map<String, Object> getChartData(String begin, String end, String type) {
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        // 设置要查询的字段
        queryWrapper.select("date_calculated",type);
        queryWrapper.between("date_calculated",begin,end);
        List<StatisticsDaily> statisticsDailyList = baseMapper.selectList(queryWrapper);

        Map<String, Object> map = new HashMap<>();
        List<Integer> dataList = new ArrayList<Integer>();
        List<String> dateList = new ArrayList<String>();
        map.put("dataList",dataList);
        map.put("dateList",dateList);

        for (StatisticsDaily statisticsDaily : statisticsDailyList) {
            dateList.add(statisticsDaily.getDateCalculated());
            switch (type){
                case "register_num":
                    dataList.add(statisticsDaily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(statisticsDaily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        return map;
    }
}